package vue;
import java.awt.BorderLayout;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceAdapter;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.InputEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.*;

import controle.ControleActionMenu;
import vue.onglets.ListeOnglets;
import vue.onglets.MenuContextuelOnglet;
import vue.util.*;

public class FenetrePrincipale extends JFrame implements DropTargetListener
{
	private static final long serialVersionUID = 1L;
	private ListeOnglets listeOnglets;	
	private BarreMenu bm;
	private BarreOutils bo;
	
	public FenetrePrincipale()
	{
		super("Simplexe");
		this.setIconImage(Util.createImage("img/logo-simplexe.png"));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocation(120,20);
		this.setSize(800,710);
		
		MenuContextuelOnglet mco = new MenuContextuelOnglet();
		listeOnglets = new ListeOnglets(mco);	
		bm = new BarreMenu(listeOnglets);
		bo = new BarreOutils(listeOnglets);		
		
		ControleActionMenu cam = new ControleActionMenu(listeOnglets,bm,bo);
		bm.ajoutItemsListener(cam);
		bo.ajoutItemsListener(cam);
		mco.ajoutItemsListener(cam);
		
		this.setLayout(new BorderLayout());		
		this.add(listeOnglets,BorderLayout.CENTER);		
		this.setJMenuBar(bm);
		this.add(bo,BorderLayout.NORTH);		
		this.setVisible(true);
		
		new DropTarget(this, this);
		
	}
	
	public ListeOnglets getListeOnglets()
	{
		return listeOnglets;
	}
	
	public BarreMenu getBarreMenu()
	{
		return bm;
	}
	
	public BarreOutils getBarreOutils()
	{
		return bo;
	}
	
	
	public void drop(DropTargetDropEvent dtde) {
		

		///
		

		try
		{
            Transferable transferable = dtde.getTransferable();

            if (transferable.isDataFlavorSupported(DataFlavor.javaFileListFlavor))
            {
				if(dtde.getDropAction()==DnDConstants.ACTION_LINK) System.out.println("link");
				if(dtde.getDropAction()==DnDConstants.ACTION_COPY) System.out.println("copy");
				if(dtde.getDropAction()==DnDConstants.ACTION_MOVE) System.out.println("move");
				if(dtde.getDropAction()==DnDConstants.ACTION_COPY_OR_MOVE) System.out.println("copy or move");

				dtde.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);

				Iterator iterator = ((List)transferable.getTransferData(DataFlavor.javaFileListFlavor)).iterator();
				while (iterator.hasNext())
				{
					File file = (File)iterator.next();
					
					JLabel label = new JLabel(file.getName(), new ImageIcon(file.getAbsolutePath()), JLabel.LEFT);

					new DragSource().createDefaultDragGestureRecognizer(label, DnDConstants.ACTION_COPY_OR_MOVE, new MyDragGestureListener(file));

					this.listeOnglets.ouvrir(new File[] {file}) ;
					//this.ouvrirFichier() ;
 					this.invalidate();
					this.invalidate();
					this.validate();
				}

				dtde.dropComplete(true);
			}
			else 
			{
				dtde.rejectDrop();
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	
		
		///
		
	}

	public void dragEnter(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void dragExit(DropTargetEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void dragOver(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	public void dropActionChanged(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
}





//trouve sur java.sun.com : http://forum.java.sun.com/thread.jspa?forumID=57&threadID=567809
class MyDragGestureListener implements DragGestureListener
{
	File file;

	MyDragGestureListener(File file)
	{
		this.file=file;
	}

	public void dragGestureRecognized(DragGestureEvent dge)
	{
		dge.startDrag(DragSource.DefaultCopyDrop, 
			new Transferable()
			{
				public Object getTransferData(DataFlavor flavor)
				{
					ArrayList<File> a = new ArrayList<File>(); // remove <File> in older java versions!
					a.add(file);
					return a;
				}
				public DataFlavor [] getTransferDataFlavors()
				{
					return new DataFlavor[]{DataFlavor.javaFileListFlavor};
				}
				public boolean isDataFlavorSupported(DataFlavor flavor)
				{
					return flavor.equals(DataFlavor.javaFileListFlavor);
				}
			},
			new DragSourceAdapter()
			{
				public void dragDropEnd(DragSourceDropEvent dsde)
				{
					if (dsde.getDropSuccess())
					{
						if (dsde.getDropAction() == DnDConstants.ACTION_LINK) System.out.println("link");
						else if (dsde.getDropAction() == DnDConstants.ACTION_MOVE) System.out.println("move");
						else System.out.println("copy");
					}
				}
				public void dropActionChanged(DragSourceDragEvent dsde)
				{
					int shift = InputEvent.SHIFT_DOWN_MASK;
					int ctrl = InputEvent.CTRL_DOWN_MASK;
				
					if ((dsde.getGestureModifiersEx() & (shift | ctrl)) == (shift | ctrl))
						dsde.getDragSourceContext().setCursor(DragSource.DefaultLinkNoDrop);
					else if ((dsde.getGestureModifiersEx() & shift) == shift)
						dsde.getDragSourceContext().setCursor(DragSource.DefaultMoveDrop);
					else
						dsde.getDragSourceContext().setCursor(DragSource.DefaultCopyDrop);
				}
			}
		);
	}
}



